package com.trainingcloud.action.user;

import com.trainingcloud.action.BaseSessionAction;
import com.trainingcloud.dao.UserDAO;
import com.trainingcloud.entity.User;
import com.trainingcloud.exception.DAOException;
import com.trainingcloud.exception.SystemException;

public class UpdateUserStatusAction extends BaseSessionAction 
{
	private String userIds;
	private String action;
	
	public UpdateUserStatusAction() throws SystemException 
	{
		super();
	}

	@Override
	public String callWithSession() throws DAOException, SystemException 
	{
		if(userIds.isEmpty())
		{
			return "Empty user id list.";
		}
		
		String[] userIdList = userIds.split(",");
		
		UserDAO userDAO = new UserDAO();
		for(int i=1; i<=userIdList.length; i++)
		{
			int userId = Integer.parseInt(userIdList[i-1]);
			
			User user = userDAO.getUserById(userId);
			if(user==null)
			{
				return "Invalid user id.";
			}
			if(action.compareTo("active")==0)
			{
				user.setActive(true);
			}
			else if(action.compareTo("unactive")==0)
			{
				user.setActive(false);
			}
			else
			{
				return "Invalid action.";
			}
			userDAO.updateUser(user);
		}
		return null;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

}
